package com.technology.education.ui.student.course

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.google.android.material.tabs.TabLayoutMediator
import com.technology.education.R
import com.technology.education.data.model.CourseCategory
import com.technology.education.databinding.FragmentCourseCategoryBinding
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.ext.view.init

/**
 *@author:create by BZY
 *Date: 2022/3/11 10
 * 课程分类页面
 */
class CourseCategoryFragment :
    BaseVmDbFragment<BaseViewModel, FragmentCourseCategoryBinding>() {
    override fun layoutId(): Int = R.layout.fragment_course_category
    override fun initView(savedInstanceState: Bundle?) {
//        mDatabind.toolBar.tvTitle.text = ""
//        mDatabind.toolBar.toolbar.setNavigationOnClickListener {
//            findNavController().navigateUp()
//        }
    }

    override fun lazyLoadData() {
        mViewModel.request({ apiService.courseCateList() }, success = {
            setUI(it)
        }, isShowDialog = true, error = {
            toast(it.errorMsg)
        })
    }

    private fun setUI(list: List<CourseCategory>) {
        mDatabind.viewPager.init(
            this,
            list.map {
                CourseListFragment.newInstance(it.children as ArrayList<CourseCategory>)
            } as ArrayList<Fragment>
        )
        TabLayoutMediator(mDatabind.tabLayout, mDatabind.viewPager) { tab, pos ->
            tab.text = list[pos].name
        }.attach()
    }

    override fun createObserver() {
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }
}